/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.view;

import br.com.sgs.entity.Etapa;
import br.com.sgs.fachada.EtapaFachada;
import br.com.sgs.fachada.EtapaIFachada;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Eric
 */

@FacesConverter(value="etapaConverter")
public class EtapaConverter implements Converter{
    
    public static List<Etapa> etapaDB;  
    
    public EtapaConverter() {  
        
        EtapaIFachada ifach = new EtapaFachada();
        etapaDB = ifach.findEtapa();
        
    }
    
    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {

        try {
           
            String descricao = submittedValue;

            for (Etapa e : etapaDB) {
                if (e.getDescricao().contains(descricao)) {
                    return e;
                }
            }

        } catch (Exception e) {
            System.err.println("Erro no getObject");
            e.printStackTrace();
            return null;
        }
        return null;
    }
    

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object value) {
       
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Etapa) value).getDescricao());
        }
    }
}
